/*
 * BanBu_CommUtils
 * @description 本文件提供半圆项目本地化操作
 * @package
 * @author 		yinlinlin
 * @copyright
 * @version 		1.0
 * @description 本文件提供半圆项目本地化操作
 */


#import "BanBu_CommUtils.h"

@implementation BanBu_CommUtils

/**
 * @函数名称：clearLocalData
 * @函数描述：根据key清除缓存
 * @输入参数：(NSString *)key
 * @输出参数：void
 * @返回值：void
 * @author：yinlinlin
 */
+ (void)clearLocalData:(NSString *)key
{
    NSUserDefaults *setting = [NSUserDefaults standardUserDefaults];
    id data = [setting objectForKey:key];
    //先移除之前的数据
    if (data)
    {
        [setting removeObjectForKey:key];
    }
}
+ (void)setLocalData:(id)value key:(NSString *)key
{
    NSUserDefaults *setting = [NSUserDefaults standardUserDefaults];
    id data = [setting objectForKey:key];
    //先移除之前的数据
    if (data)
    {
        [setting removeObjectForKey:key];
    }
    if (key) {
        [setting setObject:value forKey:key];
    }
    else
    {
        [setting removeObjectForKey:key];
    }
    [setting synchronize];
}

/**
 * getLocalData
 *
 * 根据key获取本地保存的value
 *
 * @access public
 * @param string key
 *
 * @return 返回key对应的值
 */
+ (id)getLocalData:(NSString *)key
{
    NSUserDefaults *setting = [NSUserDefaults standardUserDefaults];
    
    id data = [setting objectForKey:key];
    if (data) {
        return data;
    }
    else
    {
        return nil;
    }
}

/**
 * setCacheTime
 *
 * 设置key－value缓存开始时间
 *
 * @access public
 * @param string key        缓存的键值
 *
 * @return N/A
 */
+ (void)setCacheTime:(NSString *)key
{
    [BanBu_CommUtils setLocalData:[NSNumber numberWithFloat:[[NSDate date] timeIntervalSince1970]] key:key];
}


/**
 * needUpdateCache
 *
 * 根据key和缓存的时间判断是否需要更新缓存
 *
 * @access public
 * @param string key        缓存的键值
 * @param NSTimeInterval    timeInterval  缓存的时间
 *
 * @return 返回key对应的值
 */
+ (BOOL)needUpdateCache:(NSString *)key timeInterval:(NSTimeInterval)timeInterval
{
    NSString *value = [BanBu_CommUtils getLocalData:key];
    if (value) {
        NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] - [value intValue];
        if (interval < timeInterval) {
            return NO;
        }
    }
    return YES;
}


@end
